package com.sl.core.engine.inject;

import cn.hutool.core.annotation.AnnotationUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.sl.core.engine.annotations.Bean;

import java.lang.reflect.Field;

public class BeanInject {

    public static final BeanInject INSTANCE = new BeanInject();

    public void beanInject(Object main) {
        if (ObjectUtil.isNull(main)) {
            return;
        }
        Field[] fields = ReflectUtil.getFields(main.getClass());

        for (Field field : fields) {
            Bean annotation = AnnotationUtil.getAnnotation(field, Bean.class);
            if (ObjectUtil.isNull(annotation)) {
                continue;
            }
            Class<?> type = field.getType();
            Object o = inferEnvGetObj(type);
            ReflectUtil.setFieldValue(main, field, o);
        }
    }

    public Object inferEnvGetObj(Class<?> type) {

        //todo 暂定spring
        return SpringUtil.getBean(type);
    }
}
